<?php
class Completion_m extends CI_Model {

	/**
	 * Constructor
	 * @access public
	 * @return void
	 */
    function __construct()
    {
        parent::__construct();
    }

    /**
     * Gets the list of instrumented methods
     * @access public
     * @return array
     */
	function get_methods()
	{
		$this->db->select('method');
		$this->db->distinct();
		$query = $this->db->get('completion_time');
		
		if($query->num_rows > 0){
			$methods = array();
			$methods['all'] = 'all methods';
			foreach($query->result() as $row){
				$methods[$row->method] = $row->method;
			}
			
			return $methods;
		} else {
			return false;
		}
	}
	
	/**
	 * Returns the first and last instrumentation times since
	 * the measurements have started.
	 * @access public
	 * @return array
	 */
	function get_min_max_time()
	{
		$this->db->select_max('microtime_start');
		$maxQuery = $this->db->get('completion_time');
		
		if($maxQuery->num_rows == 0) return false;
		
		$this->db->select_min('microtime_start');
		$minQuery = $this->db->get('completion_time');
		
		if($minQuery->num_rows == 0) return false;
		
		return array('minTime' => $minQuery->row()->microtime_start,
					 'maxTime' => $maxQuery->row()->microtime_start);
	}
	
	/**
	 * Gets the last instrumented date and time minus 24hours
	 * to show as default value in the form
	 * @return int
	 */
	function get_last_time()
	{
		$this->db->select_max('microtime_start');
		$maxQuery = $this->db->get('completion_time');
		
		if($maxQuery->num_rows == 0) return false;
		
		$time = explode(".", $maxQuery->row()->microtime_start);
		
		return $time[0]-84600;
	}
	
	/**
	 * Returns the instrumented data for the given parameters. This data is then
	 * passed to the XML library for generating an XML file compatible with the chart
	 * @param int $requests
	 * @param int $over
	 * @param int $from
	 * @param String $method
	 */
	function get_data($requests, $over, $from, $method)
	{
		//If a specific query is requested, perform it, else return default
		if(!empty($requests) && !empty($over) && !empty($from) && !empty($method))
		{
			//Add condition for method
			if($method != 'all' && count(explode("::", $method)) > 0){
				$method = "AND method = '$method' ";
			} else {
				$method = null;
			}
			
			//Decrease/Increase the time value to include everything
			$from = $from - 1;
			$query = "SELECT (microtime_end - microtime_start) as completionTime, microtime_start as requestTime 
							   		   FROM completion_time 
							   		   WHERE (microtime_end - microtime_start) >= '$over' AND microtime_start > '$from' " . $method . "LIMIT 0, $requests";

			$query = $this->db->query($query);
			
		} else {
			
			//Get the timestamp 24hrs ago
			$time = time() - 84601;
			//Get the last 24h measurements over 0.5 seconds by default
			$query = $this->db->query("SELECT (microtime_end - microtime_start) as completionTime, microtime_start as requestTime 
							   		   FROM completion_time 
							   		   WHERE (microtime_end - microtime_start) >= '1' AND microtime_start > '$time' LIMIT 0, 1000");
		}
		
		return $query;

	}
	
	
}

//EOF completion_m.php